package leetcode;

import java.util.*;

/**
 * @创建人 MBY381
 * @创建时间 2022/3/25 17:41
 * @描述 暂无
 */
public class Solution0349 {
    public static int[] intersection(int[] nums1, int[] nums2) {
        List<Integer> l1 = transform(nums1);
        List<Integer> l2 = transform(nums2);
        return l1.size() < l2.size() ? cmp(l1, l2) : cmp(l2, l1);
    }

    public static List<Integer> transform(int[] nums) {
        List<Integer> l1 = new ArrayList<>();
        for (int i : nums
        ) {
            l1.add(i);
        }
        return l1;
    }

    public static int[] cmp(List<Integer> l1, List<Integer> l2) {
        Set<Integer> ans = new HashSet<>();
        for (Integer i : l1
        ) {
            if (l2.contains(i)) {
                ans.add(i);
            }
        }
        int[] ans1 = new int[ans.size()];
        int index = 0;
        for (int i : ans
        ) {
            ans1[index++] = i;
        }
        return ans1;
//        return ans.stream().mapToInt(i->i).toArray();
    }

    public static void main(String[] args) {
        int[] nums1 = new int[]{1, 2, 2, 1};
        int[] nums2 = new int[]{2, 2};
        System.out.println(Arrays.toString(intersection(nums1, nums2)));

    }
}
